package com.cssl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cssl.dao.UserMapper;
import com.cssl.entity.User;
import com.cssl.service.UserService;
import com.cssl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{

    @Autowired
    RedisUtil redisUtil;

    @Cacheable(value = "user", key = "#id", unless = "#result==null", condition="#id%2==1")
    @Override
    public User findid(int id){

        return super.getById(id);
    }

    @CachePut(value = "user", key = "#user.name", unless = "#result==null")
    @Override
    public User addid(User user){
        super.save(user);
        return user;
    }

    @CacheEvict(value = "user", key = "#id")
    @Override
    public boolean delid(int id){

        return super.removeById(id);
    }

    @Cacheable(value = "user", key = "#id")
    @Override
    public boolean updid(User user){

        return super.updateById(user);
    }

    @Override
    public List<User> show() {
        List<User> list=(List<User>) redisUtil.get("list");
        if(list==null){
            System.out.println("list=null");
            list=super.list();
            redisUtil.set("list",list);
        }
        return list;
    }
    @Override
    public boolean addval(String key,Object value){

        redisUtil.add(key,value);

        return true;
    }

    @Override
    public boolean del(String key){

        redisUtil.remove(key);

        return true;
    }
}
